<?php

namespace Platform\Api;

use PhalApi\Api;
use Platform\Domain\Banner as BannerDomain;
use PhalApi\Exception\InternalServerErrorException;

/**
 * banner管理模块
 */
class Banner extends Api {
    
    public function getRules() {
        return array(
            'bannerList' => array(
                'page' => array('name' => 'page', 'type' => 'int', 'default' => 1, 'min' => 1, 'desc' => \PhalApi\T('page')),
                'limit' => array('name' => 'limit', 'type' => 'int', 'default' => 20, 'min' => 1, 'max' => 1000, 'desc' => \PhalApi\T('page limit')),
            ),
            'addBanner'=> array(
                'banner_name' => array('name' => 'banner_name', 'require' => true, 'desc' => 'banner图'),
                'banner_url' => array('name' => 'banner_url', 'require' => true, 'desc' => 'banner地址'),
                'sort' => array('name' => 'sort', 'require' => true, 'desc' => '排序'),
            ),
            'bannerDetail'=> array(
                'id' => array('name' => 'id', 'require' => true, 'desc' => '商品id'),
            ),
            'updateBanner'=> array(
                'id' => array('name' => 'id', 'require' => true, 'desc' => '商品id'),
                'banner_name' => array('name' => 'banner_name', 'require' => true, 'desc' => 'banner图'),
                'banner_url' => array('name' => 'banner_url', 'require' => true, 'desc' => 'banner地址'),
                'sort' => array('name' => 'sort', 'require' => true, 'desc' => '排序'),
            ),
            'deleteBanner'=> array(
                'id' => array('name' => 'id', 'require' => true, 'desc' => '商品id'),
            ),
        );
    }

    /**
     * 获取banner列表
     * @desc 获取banner列表
     */
    public function bannerList() {
        $domain = new BannerDomain();
        $where = array();
        //分页
        $page = $this->page? $this->page:1;
        $perpage = $this->limit?$this->limit:10;
        //要查询的字段
        $select="id,banner_name,banner_url,from_unixtime(add_time,'%Y-%m-%d %H:%i:%S') as add_time,sort,add_who_name";
        //处理排序
        $order = 'sort DESC,id DESC';
        $roles = $domain->bannerList($where,$select,$order,$page,$perpage);
        return $roles;
    }

    /**
     * 添加banner接口
     * @desc 添加banner接口
     * @return int id banner id
     */
    public function addBanner() {
        $domain = new BannerDomain();
        //列出所有需要添加的参数
        $insertData = array();
        $insertData['banner_name'] = $this->banner_name;
        $insertData['banner_url'] = $this->banner_url;
        $insertData['sort'] = $this->sort;
        $rs = $domain->addBanner($insertData);
        return $rs;
    }

    /**
     * 获取banner详情
     * @desc 获取banner详情
     */
    public function bannerDetail() {
        $domain = new BannerDomain();
        $id = $this->id;
        $select="id,banner_name,banner_url,sort,delete_flag";
        return $domain->bannerDetail($id,$select);
    }

    /**
     * 编辑提交banner
     * @desc 编辑提交banner
     */
    public function updateBanner() {
        $domain = new BannerDomain();
        //列出所有需要添加的参数
        $id = $this->id;
        $updateData = array();
        $updateData['banner_name'] = $this->banner_name;
        $updateData['banner_url'] = $this->banner_url;
        $updateData['sort'] = $this->sort;
        return $domain->updateBanner($id,$updateData);
    }

    /**
     * 删除banner
     * @desc 删除banner
     */
    public function deleteBanner() {
        $domain = new BannerDomain();
        $id = $this->id;
        return $domain->deleteBanner($id);
    }
}
